package com.kyxx.topics.activity;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kyxx.topics.BaseActivity;
import com.kyxx.topics.BaseApp;
import com.kyxx.topics.R;
import com.kyxx.topics.net.OkGoRequest;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.ApkUpdateUtils;
import com.kyxx.topics.utils.DataCleanManage;
import com.kyxx.topics.utils.PreferenceUtil;
import com.kyxx.topics.utils.ScreenSizeUtils;
import com.kyxx.topics.utils.SettingUtils;
import com.kyxx.topics.utils.SharedPreferenceUtils;
import com.kyxx.topics.utils.SizeUtils;
import com.kyxx.topics.utils.ToastUtils;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.pgyersdk.activity.FeedbackActivity;
import com.pgyersdk.feedback.PgyFeedbackShakeManager;

import okhttp3.Call;
import okhttp3.Response;

public class SettingActivity extends BaseActivity implements View.OnClickListener {
    private Button btn_logout;
    private RelativeLayout rl_text_size;
    private TextView tv_text_size;
    private SwitchCompat s_tuisong;
    private RelativeLayout rl_clean_cache;
    private RelativeLayout rl_update;
    private SwitchCompat s_fankui;
    private TextView tv_clean_cache;
    private TextView tv_version_name;
    private TextView tv_update;
    private SettingUtils settingUtils;
    private View v_0;
    private View rl_tuisong;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_setting);
    }

    @Override
    protected void loadTopBar(String title) {
        super.loadTopBar(title);
        top_bar_tv_left.setVisibility(View.GONE);
        top_bar_iv_left.setVisibility(View.VISIBLE);
        top_bar_tv_right.setVisibility(View.GONE);
        top_bar_iv_right.setVisibility(View.GONE);
        top_bar_iv_left.setImageDrawable(getResources().getDrawable(R.drawable.ic_arrow_right));
        top_bar_iv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void findViewById() {
        loadTopBar("设置");
        btn_logout = (Button) findViewById(R.id.btn_logout);
        rl_text_size = (RelativeLayout) findViewById(R.id.rl_text_size);
        tv_text_size = (TextView) findViewById(R.id.tv_text_size);
        s_tuisong = (SwitchCompat) findViewById(R.id.s_tuisong);
        tv_clean_cache = (TextView) findViewById(R.id.tv_clean_cache);
        rl_clean_cache = (RelativeLayout) findViewById(R.id.rl_clean_cache);
        rl_update = (RelativeLayout) findViewById(R.id.rl_update);
        s_fankui = (SwitchCompat) findViewById(R.id.s_fankui);
        tv_version_name = (TextView) findViewById(R.id.tv_version_name);
        tv_update = (TextView) findViewById(R.id.tv_update);
        v_0 = findViewById(R.id.v_0);
        rl_tuisong = findViewById(R.id.rl_tuisong);
    }

    @Override
    protected void setListener() {
        btn_logout.setOnClickListener(this);
        rl_text_size.setOnClickListener(this);
        rl_clean_cache.setOnClickListener(this);
        rl_update.setOnClickListener(this);
        s_tuisong.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                settingUtils.setTuiSongTongZhi(b);
            }
        });
        s_fankui.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    PreferenceUtil.putInt("fankui", 1);
                    PgyFeedbackShakeManager.setShakingThreshold(800);
                    FeedbackActivity.setBarImmersive(true);
                    PgyFeedbackShakeManager.register(SettingActivity.this, false);
                } else {
                    PreferenceUtil.putInt("fankui", 0);
                    ToastUtils.show(context, "重启app生效");
                    PgyFeedbackShakeManager.unregister();
                }
            }
        });
    }

    @Override
    protected void processLogic() {
        if (TextUtils.isEmpty(BaseApp.getModel().getId())) {
            v_0.setVisibility(View.GONE);
            rl_tuisong.setVisibility(View.GONE);
        } else {
            v_0.setVisibility(View.VISIBLE);
            rl_tuisong.setVisibility(View.VISIBLE);
        }
        settingUtils = new SettingUtils();
        initSettings();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_logout:
                BaseApp.getModel().clear();
                finish();
                break;
            case R.id.rl_text_size:
                new AlertDialog.Builder(SettingActivity.this).setTitle("字体大小").setSingleChoiceItems(new String[]{"小", "中", "大"}, settingUtils.getZiTiDaXiao(), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        settingUtils.setZiTiDaXiao(which);
                        initSettings();
                        dialog.dismiss();
                    }
                }).setNegativeButton("取消", null).show();
                ;
                break;
            case R.id.rl_clean_cache:
                DataCleanManage.cleanApplicationData(context);
                ToastUtils.show(context, "已清除缓存");
                try {
                    tv_clean_cache.setText("0B");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.rl_update:
                ApkUpdateUtils.checkUpdate(SettingActivity.this, false);
                break;
        }
    }

    private void initSettings() {
        updateVersion();
        switch (settingUtils.getZiTiDaXiao()) {
            case 0:
                tv_text_size.setText("小");
                break;
            case 1:
                tv_text_size.setText("中");
                break;
            case 2:
                tv_text_size.setText("大");
                break;
        }
        s_tuisong.setChecked(settingUtils.isTuiSongTongZhi());
        try {
            tv_clean_cache.setText(DataCleanManage.getTotalCacheSize(context));
        } catch (Exception e) {
            e.printStackTrace();
        }
        tv_version_name.setText(ApkUpdateUtils.getVersionName(context));
        if (PreferenceUtil.getIntDefault("fankui", 0) == 0) {
            s_fankui.setChecked(false);
        } else {
            s_fankui.setChecked(true);
        }
    }

    private void updateVersion() {
        HttpParams params = new HttpParams();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("verson_code", "");
        params.put("json", jsonObject.toString());
        OkGoRequest.post(UrlUtils.appUpdate, context, "apkUpdate", CacheMode.NO_CACHE, params, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    final org.json.JSONObject result = new org.json.JSONObject(s);
                    final String versionName = result.getJSONObject("data").getString("a_verson_name");
                    tv_update.setText(versionName);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
